﻿using CRMG.Container.Resources.ExceptionResources;
using CRMG.Container.Source.BO;
using CRMG.Container.Source.Helper;
using CRMG.Container.Source.OpManager;
using CRMG.Container.Source.Variables;
using Guru.ErrorHandling;
using Guru.Framework.Util;
using Guru.TransactionLogger;
using Guru.TransactionLogger.Source.BO;
using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;

namespace CRMG.Container.Views.Lookup
{
    public partial class FrmPosition : FrmLookupBase
    {
        ResourceManager formRes;
        ResourceManager messageRes;
        LookupManager lookupMan;
        DataTable dtPositions;
        int pPstnId = 0;

        public FrmPosition() : this(-1) { }

        public FrmPosition(int positionId)
        {
            try
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(AppVariables.ApplicationCulture);
                InitializeComponent();
                formRes = new ResourceManager("CRMG.Container.Resources.ControlResources.WinFormStrings", typeof(FrmPosition).Assembly);
                messageRes = new ResourceManager("CRMG.Container.Resources.MessageResources.AppMessages", typeof(FrmPosition).Assembly);
                lookupMan = new LookupManager();
                dtPositions = lookupMan.GetPositions();
                pPstnId = positionId;
                this.Text = formRes.GetString("FrmPositions.Caption");
                this.lblName.Text = formRes.GetString("FrmPositions.lblPosition");
                this.btnSave.Text = pPstnId != -1 ? formRes.GetString("Update") : formRes.GetString("Add");
                this.btnCancel.Text = formRes.GetString("Cancel");

                if (pPstnId != -1)
                {
                    this.txtName.Text = DataUtil.GetObjectWithSelectedColumn(dtPositions, "PositionName", pPstnId, "OBJID").ToStr();
                }
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                          messageRes.GetString("FrmPositions.Ctor.Error"),
                          messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2048,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2048)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmPositions.Ctor.Error"),
                      messageRes.GetString("Error"));
            }
            finally
            {
                btnSave.Enabled = false;
            }
        }


        protected override void SaveClick(object sender, EventArgs e)
        {
            try
            {
                base.SaveClick(sender, e);

                int GrdId = DataUtil.GetObjectWithSelectedColumn(dtPositions, "PositionName", txtName.Text, "OBJID").ToInt();
                if (GrdId != pPstnId && GrdId > 0)
                {
                    ControlUtil.ShowWarningMessage(
                    messageRes.GetString("FrmPositions.Save.CompanyAlreadyExist"),
                    messageRes.GetString("Warning"));
                    return;
                }
                else
                {
                    Position pst = new Position() { PositionName = txtName.Text };
                    int Id = 0;
                    if (pPstnId == -1)
                    {
                        pst.IsActive = 1;
                        Id = lookupMan.AddPosition(pst);
                    }
                    else
                    {
                        pst.OBJID = pPstnId;
                        lookupMan.UpdatePosition(pst);
                    }
                    TransactionLogEntry transLogger = new TransactionLogEntry(AppVariables.CurrentUser.OBJID)
                    {
                        FormName = this.Name,
                        TransactionType = pPstnId == -1 ? TransactionTypes.Insert : TransactionTypes.Update,
                        ControlName = btnSave.Text,
                        ControlTag = btnSave.Tag.ToStr(),
                        LogObject = pPstnId == -1 ? Id : pPstnId
                    };
                    transLogger.Insert();

                    btnSave.Enabled = false;
                    btnCancel.Text = formRes.GetString("Close");
                    ControlUtil.ShowInfoMessage(
                      messageRes.GetString("FrmPositions.Save.Success"),
                      messageRes.GetString("Info"));
                }
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                          messageRes.GetString("FrmPositions.Save.Error"),
                          messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2049,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2049)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmPositions.Save.Error"),
                      messageRes.GetString("Error"));
            }

        }

    }
}
